package com.hmall.user.web;

import com.hmall.common.client.AddressAndUserClient;
import com.hmall.common.pojo.Order;
import com.hmall.common.pojo.User;
import com.hmall.user.mapper.UserMapper;
import com.hmall.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Resource
    private UserMapper userMapper;
    @Autowired
    AddressAndUserClient addressAndUserClient;


    @GetMapping("/{id}")
    public User findUserById(@PathVariable("id") Long id) {
        return userMapper.selectById(id);
    }

    //    扣减余额
    @PostMapping
    public int updataById(@RequestBody User user) {
        return userMapper.updateById(user);
    }

    //    取消订单回款
    @PostMapping("/user/returnMoney")
    public int returnMoney(@RequestBody Order order) {
//        User user = addressAndUserClient.findUserById(order.getUserId());
        User user = userMapper.selectById(order.getUserId());
        user.setBalance(user.getBalance() + order.getTotalFee());
        return addressAndUserClient.updataById(user);
    }


}
